package GUI;

import beans.User;
import service.LogService;

import javax.swing.*;
import java.awt.*;

public class AdminLoginFrame extends JFrame {
    JFrame loginFrame = new JFrame("管理员登录");
    JLabel nameLabel = new JLabel("账号：");
    JLabel passwordLabel = new JLabel("密码：");
    JTextField nameField = new JTextField(20);
    JTextField passwordField = new JTextField(20);
    JButton loginButton = new JButton("管理员登录");

    JButton returnButton = new JButton("返回");
    JPanel namePanel = new JPanel();
    JPanel passwordPanel = new JPanel();
    JPanel buttonPanel = new JPanel();

    public void AdminLoginFrame(){
        namePanel.add(nameLabel);
        namePanel.add(nameField);
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);
        buttonPanel.add(loginButton);
        buttonPanel.add(returnButton);

        loginFrame.add(namePanel, BorderLayout.NORTH);
        loginFrame.add(passwordPanel,BorderLayout.CENTER);
        loginFrame.add(buttonPanel,BorderLayout.SOUTH);
        loginFrame.setSize(500,200);
        loginFrame.setLocationRelativeTo(null);
        loginFrame.setVisible(true);
        loginFrame.setDefaultCloseOperation(2);

        //管理员登录
        loginButton.addActionListener(e -> {
            String name = nameField.getText();
            String password = passwordField.getText();

            if (name.equals("admin") && password.equals("000000")){
                JOptionPane.showMessageDialog(null,"管理员登陆成功");
                new AdminFrame().AdminFrame();
                loginFrame.dispose();
                User admin = new User();
                LogService.addLogs(admin,"管理员 " + admin + "登录成功");
            }else {
                JOptionPane.showMessageDialog(null,"账号或密码错误，请重新输入！！！");
                return;
            }
        });

        returnButton.addActionListener(e -> {
            loginFrame.dispose();
            new Menu().Menu();
        });
    }
}
